//
//  NativeCollectionViewModel.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

class NativeCollectionViewModel: NSObject {

   private var _sections: [UICollectionViewSectionModelProtocol] = []
    
    func requestNativeAds() {
        let natives = kNativeAds.json_deserialize(model: NativeAdModel.self)
        
        var sections: [UICollectionViewSectionModelProtocol] = []
        let colum = 2
        let section = STNativeAdSectionModel()
        let horizontalInsets = section.layoutEdgeinset.st_horizontal
        let spacing = section.minimumInteritemSpacing
        let maxWidth = CGFloat.dv_width - horizontalInsets
        let minWidth = (maxWidth - spacing * CGFloat(colum - 1)) / 2
        
        natives.forEach({ nativeModel in
            let random = arc4random_uniform(3)
            let width = random == 0 ? maxWidth : minWidth
            let height = (width * 9 / 16) + 40
            let size = CGSize(width: width, height: height)
            let cellModel = NativeAdCellModel(nativeModel, cellSize: size)
            section.append(item: cellModel)
        })
        
        sections.append(section)
        _sections = sections
    }
}

extension NativeCollectionViewModel: UICollectionViewModelProtocol {
    
    var sections: [UICollectionViewSectionModelProtocol] {
        return _sections
    }
}
